const express = require('express');

let server = express();
server.listen(8080);

let articleRouter = express.Router();   // 创建一个路由

articleRouter.get('/', (req, res) => {    // article的首页   http://xxx.com/article/
    res.send('文章首页');
});
articleRouter.get('/:id', (req, res) => {    // 带有id参数的文章页   http://xxx.com/article/1231231
	let id = req.params.id;		          // req.params就是参数。
	res.send(`${id}文章内容`);
});
articleRouter.get('/:id/comment', (req, res) => {
	res.send('文章评论');
});
articleRouter.get('/:id/edit', (req, res) => {
	res.send('编辑文章');
});

let aaaRouter = express.Router();
aaaRouter.get('/a', (req, res) => {     // article下的子路由   http://xxx.com/article/aaa/a
    res.send('a');
});
aaaRouter.get('/b', (req, res) => {
    res.send('b');
});
aaaRouter.get('/c', (req, res) => {
    res.send('c');
});

let userRouter = express.Router();
userRouter.get('/', (req, res) => {    // 用户信息   http://xxx.com/user/2210
    res.send('用户首页');
});
userRouter.get('/:id', (req, res) => {    // 用户信息   http://xxx.com/user/2210
    res.send('用户信息');
});
userRouter.get('/:id/comment', (req, res) => {
    res.send('用户发的评论');
});

server.use('/article', articleRouter);   // 把路由添加给父级server
server.use('/user', userRouter);

articleRouter.use('/aaa', aaaRouter);   // 不止server能用use，路由也可以使用use方法，也就是在路由的内部再套一层路由






